package cn.tedu._05mvcboot02.controller;

import cn.tedu._05mvcboot02.mapper.UserMapper;
import cn.tedu._05mvcboot02.pojo.dto.InsertDTO;
import cn.tedu._05mvcboot02.pojo.entity.User;
import cn.tedu._05mvcboot02.pojo.vo.UserListVO;
import cn.tedu._05mvcboot02.pojo.vo.UserVO;
import cn.tedu._05weibo.base.response.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@Api(tags = "用户模块")
@RequestMapping("/v1/users/")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("insert")
    @ApiOperation("插入功能")
    public JsonResult insert(InsertDTO insertDTO) {
        User user = new User();
        BeanUtils.copyProperties(insertDTO, user);
        user.setCreated(new Date());
        userMapper.insert(user);
        return JsonResult.ok(user);
    }

    @GetMapping("userList")
    @ApiOperation("查询功能")
    public List<UserListVO> userList() {
        return userMapper.userList();


    }

    @GetMapping("delete")
    @ApiOperation("删除功能")
    public JsonResult delete(Long id) {
        userMapper.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("update")
    @ApiOperation("更新功能")
    public JsonResult update(User user) {
        userMapper.update(user);
        return JsonResult.ok();
    }

    @GetMapping("selectById/{uid}")
    @ApiOperation("用户id查询功能")
    @ApiImplicitParam(name = "uid",value = "用户id",required = true,dataType = "int")
    public UserVO selectById(@PathVariable Integer uid){
        return userMapper.selectById(uid);
    }





}
